package main

import (
	"io"
	"net/http"
	"net/rpc"
	"net/rpc/jsonrpc"
)

type HelloService struct {
}

//函数必须这样定义（参数类型）
func (s *HelloService) Hello(request string, reply *string) error {
	//返回值是通过修改replay的值
	*reply = "hello, " + request
	return nil
}

//将传输协议换成http
func main() {
	// 1.实例化一个server
	_ = rpc.RegisterName("HelloService", &HelloService{})
	http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request){
		var conn io.ReadWriteCloser = struct {
			io.Writer
			io.ReadCloser
		}{
			ReadCloser: r.Body,
			Writer: w,
		}
		_ = rpc.ServeRequest(jsonrpc.NewServerCodec(conn))
	})
	_ = http.ListenAndServe(":1234", nil)
}


//client写在python中了，就是一个http请求
//参见pytesting go_json_rpc_client.py http_json_rpc